package cn.zysuyuan.item.service.impl;

import cn.zysuyuan.common.entity.Constant;
import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.utils.UUIDUtil;
import cn.zysuyuan.item.entity.Medicinal;
import cn.zysuyuan.item.mapper.MedicinalMapper;
import cn.zysuyuan.item.service.MedicinalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.management.ObjectName;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2019-11-18 14:48
 */
@Service
public class MedicinalServiceImpl implements MedicinalService {

    @Autowired
    private MedicinalMapper medicinalMapper;

    private String imagePath;

    @Override
    public PageResult<Medicinal> selectList(Map<String, Object> map) {
        List<Medicinal> medicinalList = this.medicinalMapper.selectList(map);
        return new PageResult<Medicinal>(medicinalList);
    }

    @Override
    public int insert(Medicinal medicinal) {
        medicinal.setId(UUIDUtil.uuid());
        medicinal.setmCreateTime(new Date());
        return medicinalMapper.insert(medicinal);
    }

    @Override
    public int update(Medicinal medicinal) {

        /**
         * 判断是否有图片更新，有图片更新时，则删除原有的图片，只有选择新的图片时，才给imgPath赋予新值，并且选中后的图片和选中前的不一样
         */
        String imgpaths = medicinal.getImgPath();
        if (imgpaths != null && !"".equals(imgpaths) && !imgpaths.equals(medicinal.getImgPathOut())) {
            String paths = Constant.IMG_PATH + medicinal.getImgPathOut();
            File files = new File(paths);
            if (files.exists()) {
                files.delete();
            }
        }

        String imgoriginpath = medicinal.getmOriginImgPath();
        if (imgoriginpath != null && !"".equals(imgoriginpath) && !imgoriginpath.equals(medicinal.getImgOriginPathOut())) {
            String pathOrigin = Constant.IMG_PATH + medicinal.getImgOriginPathOut();
            File file = new File(pathOrigin);
            if (file.exists()){
                file.delete();
            }
        }

        return medicinalMapper.update(medicinal);
    }

    @Override
    public int delete(String id,String imgPath,String mOriginImgPath) {
        /**
         * 删除药品信息时信息首先要把文件中相应的图片删除
         */
        String path = Constant.IMG_PATH + imgPath;
        File file = new File(path); //药材图片
        if (file.exists()) {
            file.delete();
        }

        String pathOrigin = Constant.IMG_PATH + mOriginImgPath;
        File fileOrigin = new File(pathOrigin); // 基源图片
        if (fileOrigin.exists()) {
            fileOrigin.delete();
        }

        // 删除对应药材的公司

        // 删除对应药材基地关联表中与这个药材相关的记录

        // 删除基地中这种药材的原料去向

        return medicinalMapper.delete(id);
    }

    @Override
    public Map<String, Object> saveImg(MultipartFile file,Map<String, Object> map) {
        Map<String,Object> maps = new HashMap<String, Object>();
        try {
            //判断是否拥有路径，没有就创建
            String path = Constant.IMG_PATH_FILE;
            if (!new File(path).exists()) {
                new File(path).mkdirs();
            }
            String image = uploadFile(file,path,map);
            System.out.println("+++++++++++++++++image:" + image);
            maps.put("flag",true);
        } catch (Exception e) {
            maps.put("flag",false);
            e.printStackTrace();
        }
        return maps;
    }

    public static String uploadFile(MultipartFile file, String path,Map<String, Object> map) throws IOException {
//        String imagename = UUIDUtil.uuid();
//        System.out.println("imagename+++++++++++:" + imagename);
        String name = file.getOriginalFilename();
        String suffixName = name.substring(name.lastIndexOf("."));
        String fileName = map.get("imgname") + suffixName;
        File tempFile = new File(path,fileName);
        // getParentFile()：获取目录
        if (!tempFile.getParentFile().exists()) {
            tempFile.getParentFile().mkdir();
        }
        if (tempFile.exists()) {
            tempFile.delete();
        }
        tempFile.createNewFile();
        // 将file文件内容写入新创建的磁盘空间中
        file.transferTo(tempFile);
        return tempFile.getName();
    }

}
